//获得游戏窗口的宽度
const gameWidth = document.querySelector('.game').clientWidth
gameDom = document.querySelector('.game')

class Pipe extends Rectangle {
    constructor(height, top, xspeed, dom) {
        //上下水管不同的需传参，相同的且确定的 直接继承父类构造方法
        super(52, height, gameWidth, top, xspeed, 0, dom)
    }

    //pipe超出屏幕范围时，删除pipe
    onMove() {
        if (this.left < -this.width) {
            //移除dom
            this.dom.remove()
        }
    }
}

//随机函数
function getRandom(min, max) {
    return Math.floor(Math.random() * (max - min) + min);
}

//水管对,上下一对水管
//这个类不是矩形了，就不能继承Rectangle了
//确定高度   空隙确定150px  通过计算得出两水管高度
class PipePair {
    constructor(xspeed) {
        //空隙高度
        this.spaceHeight = 150
        //随机上水管高度
        this.minHeight = 80   //上水管最小高度
        this.maxHeight = landtop - this.minHeight - this.spaceHeight
        //上水管高度
        const upHeight = getRandom(this.minHeight, this.maxHeight)
        const downHeight = landtop - upHeight - this.spaceHeight
        const downTop = landtop - downHeight

        //创建水管dom
        const upDom = document.createElement('div')
        upDom.className = 'pipe up'


        const downDom = document.createElement('div')
        downDom.className = 'pipe down'

        this.upPipe = new Pipe(upHeight, 0, xspeed, upDom) //上水管 , 创建水管对象
        this.downPipe = new Pipe(downHeight, downTop, xspeed, downDom)//下水管

        //添加dom到游戏窗口
        gameDom.appendChild(upDom)
        gameDom.appendChild(downDom)
    }

    move(duration) {
        this.upPipe.move(duration)
        this.downPipe.move(duration)
    }

    //判断无用数组元素,再pairs数组中删除
    get useless() {
        return this.upPipe.left < -this.upPipe.width
    }
}

//产生水管对的类 , 用于不断地产生水管对
class PipePairProducer {
    constructor(xspeed) {
        this.xspeed = xspeed
        this.pairs = []
        this.timer = null
        this.tick = 1500     //1500ms产生一对水管对
    }

    startProduce() {
        //如果timer有值，则结束方法
        if (this.timer) {
            return
        }
        //为防止this混乱，使用 箭头 来解决this的指向问题
        this.timer = setInterval(() => {
            this.pairs.push(new PipePair(this.xspeed))

            //判断无用数组元素,再pairs数组中删除
            for (let i = 0; i < this.pairs.length; i++) {
                if (this.pairs[i].useless) {
                    this.pairs.splice(i, 1)
                    //i--使得当数组内多个没用时，用splice删除时，
                    //后一个会向前一个下标，i不增加，防止漏下
                    i--
                }
            }
        }, this.tick);
    }

    stopProduce() {
        clearInterval(this.timer)
        this.timer = null
    }
}


// 开启创建水管对
let producer = new PipePairProducer(-100)   //传入水管速度
producer.startProduce()

//移动
setInterval(function () {
    // 用foreach作用所有水管对
    producer.pairs.forEach(function (pair) {
        pair.move(16 / 1000)

        //碰撞判定
        if (pair.upPipe.left <= bird.left + bird.width && pair.upPipe.left + pair.upPipe.width >= bird.left) {
            if (bird.top >= pair.upPipe.height && bird.top <= pair.downPipe.top) {

            } else {
                stopGame()
                //显示gameover
                document.querySelector('.gameOver').style.display = 'block'
            }
        }
    })
}, 16)